var token=localStorage.getItem("token");
var phoneNum=localStorage.getItem("phoneNum");
var openid = '';


$(document).ready(function(){
    var paramStr = {};
    paramStr.phoneNum =phoneNum;
    var params = {};
    params.paramStr = JSON.stringify(paramStr);
    var flag = false;
    $.ajax({
        headers: {"phone":phoneNum,"token":token},
        method: "POST",
        data:params,
        url:'/fenful/api/v1/user/getUser',
        datatype: "json",
        async: flag,
        success: function(re) {
            var data=JSON.parse(re.data);
            console.log(data);

            $('#uImg').attr("src",data.headImg);
            $('.uname').text(data.nickName);

            //会员图片
            var vipImg = "wzlh5/imgs/ui/";
            var memberGrade = data.memberGrade;
            if(memberGrade=="1"){
                vipImg+="vip_1.png";
            }else if(memberGrade=="2"){
                vipImg+="vip_2.png";
            }else if(memberGrade=="3"){
                vipImg+="vip_3.png";
            }
            $(".vip-level").attr("src",vipImg);
            //会员到期时间
            if(data.memberGradeEfTime&&memberGrade!="1"){
                $(".vip-endtime").text("会员到期:"+data.memberGradeEfTime.substring(0,10));
            }
            fitBtn(memberGrade);
        },
        error: function(re) {
            console.log(re);
        }
    });
    getopenid();
});
//判断升级按钮，参数当前会员等级
function fitBtn(memberGrade) {
    if(memberGrade=="3"){
        //当前是v3只有续费
        $("#v2_btn").remove();
        $("#v3_btn").attr("data-buy","11");//10-普通升会员；11-续费；13-V2升V3
        $("#v3_btn").html("续费V3&nbsp;&nbsp;￥188元");
    }else if(memberGrade=="2"){
        $("#v3_btn").attr("data-buy","13");
        $("#v3_btn").attr("data-value","60");
        $("#v3_btn").html("升级V3&nbsp;&nbsp;￥60元");

        $("#v2_btn").attr("data-buy","11");
        $("#v2_btn").html("续费V2&nbsp;&nbsp;￥128元");

    }
}

function buyVip(me) {
    var upgrade = $(me).attr("data-type");
    var amount = $(me).attr("data-value");
    // var amount = 0.01;
    var trade_id = $(me).attr("data-buy");
    var phoneNum= window.localStorage.getItem("phoneNum");
    if(!phoneNum){
        window.location.href="/fenful/login.html?_path=/fenful/vipPlus.html";
        return;
    }

    layer.confirm("您选择了V"+upgrade+"  需要支付￥"+amount+"元",{title:"请确认"},function (index) {
        var paramStr = {};
        paramStr.phoneNum = phoneNum;
        paramStr.openid = openid;
        paramStr.totalAmount = amount;
        paramStr.typeId =trade_id;
        paramStr.upgrade=upgrade;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        Common.ajaxWithParam("/fenful/api/v1/common/weixinCreateOrder", params, function(data) {
            console.log(data);
            var paramStr = {};
            paramStr.appId = data.appId;
            paramStr.timeStamp = data.timeStamp;
            paramStr.nonceStr = data.nonce_str;
            paramStr.package = data.prepay_id;
            paramStr.signType = data.signType;
            paramStr.paySign = data.sign;
            recharge(paramStr);
        });

        layer.close(index);
    });
}
function recharge(paramStr) {

    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId": paramStr.appId, //公众号名称，由商户传入
            "timeStamp": paramStr.timeStamp, //时间戳，自1970年以来的秒数
            "nonceStr": paramStr.nonceStr, //随机串
            "package": paramStr.package,
            "signType": paramStr.signType, //微信签名方式：
            "paySign": paramStr.paySign //微信签名
        },
        function(result) {
            if(result.err_msg == "get_brand_wcpay_request:cancel") {
                layer.alert("您取消了支付！");
            } else {
                layer.alert("支付成功！");
                window.location.reload();
            }
        }
    );
}
function getopenid() {
    openid = window.localStorage.getItem("openid");
    if(openid == null || openid == undefined || openid == '') {
        getCode();
    }
}

function getCode() {
    var code = Common.queryGetParam('code');
    if(code == null || code == undefined || code == '') {
        location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95bdc635798fcfe4&redirect_uri="+window.location.href+"&response_type=code&scope=snsapi_base#wechat_redirect";
        return;
    }
    var paramStr = {};
    paramStr.code = code;
    var params = {};
    params.paramStr = JSON.stringify(paramStr);
    Common.ajaxWithParam("/fenful/api/v1/common/getWxUserOpenId", params, function(data) {
        console.log(data);
        openid = data.openid;
        window.localStorage.setItem("openid", data.openid);
    });
}